package com.tepth.maintenancesystem.view;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.superrecycleview.superlibrary.adapter.SuperBaseAdapter;
import com.superrecycleview.superlibrary.recycleview.ProgressStyle;
import com.superrecycleview.superlibrary.recycleview.SuperRecyclerView;
import com.tepth.maintenancesystem.R;
import com.tepth.maintenancesystem.adapter.MyDataAdapter;
import com.tepth.maintenancesystem.bean.VehicleObj;
import com.tepth.maintenancesystem.untils.ToastUtils;
import com.tepth.maintenancesystem.view.fragment.SearchStaticListSupportFragment;
import com.xdandroid.materialprogressview.MaterialProgressView;

import net.cachapa.expandablelayout.ExpandableLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import xyz.sahildave.widget.SearchViewLayout;

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.visible_bar_number_done)
    TextView visible_bar_number_done;

    @BindView(R.id.visible_bar_number_doing)
    TextView visible_bar_number_doing;

    @BindView(R.id.visible_bar_number_ready)
    TextView visible_bar_number_ready;

    @BindView(R.id.vehicle_list)
    SuperRecyclerView vehicle_list;

    @BindView(R.id.maintain_loading_layout)
    LinearLayout maintain_loading_layout;

    @BindView(R.id.progress_view)
    MaterialProgressView progressView;

    @BindView(R.id.nodata_notification)
    LinearLayout nodata_notification;

    private List<VehicleObj> datas;//保养总集合
    private Context context;
    private MyDataAdapter myAdapter;//保养列表适配器

    private SearchViewLayout searchViewLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        context = this;
        datas = new ArrayList<>();
        addData();
        initView();
        initAdapter();
    }

    private void initView() {
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);

        searchViewLayout = (SearchViewLayout) findViewById(R.id.search_view_container);
        searchViewLayout.setExpandedContentSupportFragment(this, new SearchStaticListSupportFragment());
        searchViewLayout.handleToolbarAnimation(toolbar);
        searchViewLayout.setCollapsedHint("请输入车辆编号");
        searchViewLayout.setExpandedHint("请输入车辆编号");

        ColorDrawable collapsed = new ColorDrawable(ContextCompat.getColor(context, R.color.colorPrimary));
        ColorDrawable expanded = new ColorDrawable(ContextCompat.getColor(context, R.color.default_color_expanded));
        searchViewLayout.setTransitionDrawables(collapsed, expanded);
        searchViewLayout.setSearchListener(new SearchViewLayout.SearchListener() {
            @Override
            public void onFinished(String searchKeyword) {

            }
        });
        searchViewLayout.setOnToggleAnimationListener(new SearchViewLayout.OnToggleAnimationListener() {
            @Override
            public void onStart(boolean expanding) {

            }

            @Override
            public void onFinish(boolean expanded) {

            }
        });
        searchViewLayout.setSearchBoxListener(new SearchViewLayout.SearchBoxListener() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        vehicle_list.setLayoutManager(layoutManager);
        vehicle_list.setRefreshEnabled(true);//可以定制是否开启下拉刷新
        vehicle_list.setLoadMoreEnabled(true);//可以定制是否开启加载更多
        vehicle_list.setLoadingListener(new PullToRefreshListener());//下拉刷新，上拉加载的监听
        vehicle_list.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);//下拉刷新的样式
        vehicle_list.setLoadingMoreProgressStyle(ProgressStyle.BallClipRotate);//上拉加载的样式
        vehicle_list.setArrowImageView(R.drawable.arrow_down);//设置下拉箭头
    }

    @OnClick({R.id.img_home_setting, R.id.img_home_barcode, R.id.main_maintain_bar_layout_all, R.id.main_maintain_bar_layout_ready, R.id.main_maintain_bar_layout_doing, R.id.main_maintain_bar_layout_done})
    public void onclick(View view) {
        if (searchViewLayout.isExpanded())
            return;
        switch (view.getId()) {
            case R.id.main_maintain_bar_layout_all:
//                initAdapter();
                break;
            case R.id.main_maintain_bar_layout_ready:
//                initAdapter();
                break;
            case R.id.main_maintain_bar_layout_doing:
//                initAdapter();
                break;
            case R.id.main_maintain_bar_layout_done:
//                initAdapter();
                break;
            case R.id.img_home_setting:
//                Intent intent = new Intent();
//                intent.setClass(this, SettingActivity.class);
//                startActivity(intent);
                ToastUtils.showToast(context, "该功能暂未开放");
                break;
            case R.id.img_home_barcode:
                ToastUtils.showToast(context, "该功能暂未开放");
                break;
        }
    }

    private void initAdapter() {
        myAdapter = new MyDataAdapter(context, datas);
        vehicle_list.setAdapter(myAdapter);
        myAdapter.setOnItemClickListener(new SuperBaseAdapter.OnItemClickListener<VehicleObj>() {
            @Override
            public void onItemClick(View view, VehicleObj item, int position) {
                ExpandableLayout expandableLayout = (ExpandableLayout) view.findViewById(R.id.expandable_layout);
                item.setExpanded(!item.isExpanded());
                expandableLayout.setExpanded(!item.isExpanded());
//                Intent intent=new Intent(context,VehicleDetailsActivity.class);
//                startActivity(intent);
            }
        });
    }

    /**
     * 添加数据
     */
    private void addData() {
        datas.add(new VehicleObj("81213", "混合动力  138", "2016/11/28 10:08", "1", "5", "0"));
        datas.add(new VehicleObj("81212", "混合动力  138", "2016/11/28 10:08", "1", "3", "0"));
        datas.add(new VehicleObj("81211", "混合动力  140", "2016/11/28 10:08", "2", "7", "1"));
        datas.add(new VehicleObj("81210", "混合动力  140", "2016/11/28 10:08", "3", "6", "1"));

        datas.add(new VehicleObj("81213", "混合动力  138", "2016/11/28 10:08", "0", "5", "2"));
        datas.add(new VehicleObj("81212", "混合动力  138", "2016/11/28 10:08", "0", "3", "2"));
        datas.add(new VehicleObj("81211", "混合动力  140", "2016/11/28 10:08", "0", "7", "3"));
        datas.add(new VehicleObj("81210", "混合动力  140", "2016/11/28 10:08", "0", "6", "3"));
    }

    public void newData() {
        datas.clear();
        addData();
    }

    private class PullToRefreshListener implements SuperRecyclerView.LoadingListener {

        @Override
        public void onLoadMore() {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    addData();
                    vehicle_list.completeLoadMore();
                }
            }, 3000);
        }

        @Override
        public void onRefresh() {
//            if(dataList.size() >= 50){
//                superRecyclerView.setNoMore(true);
//                return;
//            }
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    newData();
                    vehicle_list.completeRefresh();
                    myAdapter.notifyDataSetChanged();
                }
            }, 3000);
        }
    }
}
